//
//  WWDataParser.m
//  WangLottery
//
//  Created by 黄修勇 on 12-9-11.
//
//

#import "WWDataParser.h"
#import "RegexKitLite.h"


@implementation WWDataParser

//判断结果是否符合规范

-(NSDictionary *)dicForResult:(NSDictionary *)dic{

    if (dic.allKeys.count>0) {
        return dic;
    }else
        return nil;
}

// 返回数据中只存在2个key的

-(NSDictionary *)simpleResultForString:(NSString *)string{

    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    id tempResult=[string parserData];
    if ([tempResult isKindOfClass:[NSString class]]) {
        NSArray *tempArray=[tempResult componentsSeparatedByString:@","];
        if (tempArray.count>=2) {
            [dic setObject:[tempArray objectAtIndex:0] forKey:@"result"];
            [dic setObject:[tempArray objectAtIndex:1] forKey:@"reason"];
        }
    }
    return [self dicForResult:dic];
}

-(NSDictionary *)simpleResultForString:(NSString *)string keyArray:(NSArray *)keyArray{

    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    id tempResult=[string parserData];
    if ([tempResult isKindOfClass:[NSString class]]) {
        NSArray *tempArray=[tempResult componentsSeparatedByString:@","];
        for (int i=0; i<keyArray.count; i++) {
            [dic setObject:[tempArray objectAtIndex:i] forKey:[keyArray objectAtIndex:i]];
        }
    }
    return [self dicForResult:dic];
}

-(NSArray *)resultArrayByKeyArray:(NSArray *)keyArray dataArray:(NSArray *)data{
    
    NSMutableArray *tempArray=[[NSMutableArray new] autorelease];
    for (int i=0; i<data.count; i++) {
        NSString *tempString=[data objectAtIndex:i];
        if (tempString.length>2) {
            NSString *dataString=[tempString substringWithRange:NSMakeRange(1, tempString.length-2)];
            NSArray *tempDataArray=[dataString componentsSeparatedByString:@","];
            NSMutableDictionary *tempDic=[NSMutableDictionary dictionary];
            
            for (int j=0; j<tempDataArray.count; j++) {
                [tempDic setObject:[tempDataArray objectAtIndex:j] forKey:[keyArray objectAtIndex:j]];
            }
            [tempArray addObject:tempDic];
        }
    }
    return tempArray;
}


-(NSArray *)resultArrayByKeyArray:(NSArray *)keyArray dataModel:(id)model{

    NSArray *modelArray=[(WWDataModel *)model dataArray];
    return [self resultArrayByKeyArray:keyArray dataArray:modelArray];
}


-(NSDictionary *)resultDicByKeyArray:(NSArray *)keyArray dataModel:(id)model{

    return [self resultDicByKeyArray:keyArray dataModel:model infoKeyArray:[NSArray arrayWithObjects:@"result",@"reason", nil]];
}

-(NSDictionary *)resultDicByKeyArray:(NSArray *)keyArray dataModel:(id)model infoKeyArray:(NSArray *)infoKeyArray{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    WWDataModel *tempModel=(WWDataModel *)model;
    NSArray *tempArray=[self resultArrayByKeyArray:keyArray dataModel:model];
    [dic setObject:tempArray forKey:@"data"];
    NSArray *infoArray=[[tempModel infoString] componentsSeparatedByString:@","];
    
    for (int i=0; i<infoKeyArray.count; i++) {
        [dic setObject:[infoArray objectAtIndex:i] forKey:[infoKeyArray objectAtIndex:i]];
    }
    return [self dicForResult:dic];
}

-(NSDictionary *)resultDicByKeyArray:(NSArray *)keyArray string:(NSString *)string{

    id tempResult=[string parserData];
    if ([tempResult isKindOfClass:[WWDataModel class]]) {
        
        return [self resultDicByKeyArray:keyArray dataModel:tempResult];
    }
    return nil;
}

-(NSDictionary *)resultDicByKeyArray:(NSArray *)keyArray string:(NSString *)string infoKeyArray:(NSArray *)infoKeyArray{

    id tempResult=[string parserData];
    if ([tempResult isKindOfClass:[WWDataModel class]]) {
        
        return [self resultDicByKeyArray:keyArray dataModel:tempResult infoKeyArray:infoKeyArray];
    }
    return nil;
}
@end


@implementation NSString (WWDataParser)

-(id)parserData{

    NSString *regString=@"\\[(.*?)\\]";
    if (self.length<2) {
        return nil;
    }
    
    NSMutableString *tempNewString=[NSMutableString stringWithString:[self substringWithRange:NSMakeRange(1, self.length-2)]];
    if ([tempNewString isMatchedByRegex:regString]) {
        NSArray *tempArray=[tempNewString componentsMatchedByRegex:regString];
        WWDataModel *model=[[WWDataModel alloc]init];
        model.dataArray=tempArray;
        
        for (NSString *string in tempArray) {
            [tempNewString stringByReplacingOccurrencesOfString:string withString:@""];
        }
        if (tempNewString.length>1) {
            if([[tempNewString substringFromIndex:tempNewString.length-1] isEqualToString:@","]){
                model.infoString=[tempNewString substringToIndex:tempNewString.length-1];
            }
        }
        return model;
    }else
        return tempNewString;
    
    return nil;
}
@end


@implementation WWDataModel

@synthesize infoString=_infoString;
@synthesize dataArray=_dataArray;

-(id)init{

    self=[super init];
    if (self) {
        self.infoString=nil;
        self.dataArray=nil;
    }
    return self;
}

-(void)dealloc{

    [_infoString release];
    [_dataArray release];
    [super dealloc];
}

@end
